from flask import Flask,render_template,request
import time
import sqlite3
app =Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html' )

@app.route('/select',methods=['POST'])
def selects():
    name =request.form.get("name")
    con = sqlite3.connect("liuyan.db")  # 建立数据库连接
    con.row_factory = sqlite3.Row  # 设置row_factory,对查询到的数据，通过字段名获取列数据
    cur = con.cursor()  # 获取游标
    cur.execute("select * from card where name=? ",(name,))  # 执行sql语句选择数据表
    rows = cur.fetchall()  # 获取多条记录数据
    con.close()
    return render_template('cha.html', rows=rows)

@app.route('/add',methods=['POST'])
def adds():
    name=request.form.get("name")#获取前端送过来的name数据
    text=request.form.get("text")#获取内容
    tim=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())#时间格式
    con =sqlite3.connect("liuyan.db")#连接数据库
    cur =con.cursor()
    cur.execute("insert into card (name,text,time) values (?,?,?)",(name,text,tim))#向数据库插入一条记录
    con.commit()
    con.close()
    return render_template('add.html',mgs="发送成功")
if __name__ == '__main__':
    app.run()
    adds()